package cn.tedu.baking.service.impl;

import cn.tedu.baking.exception.ServiceException;
import cn.tedu.baking.mapper.CommentMapper;
import cn.tedu.baking.mapper.ContentMapper;
import cn.tedu.baking.mapper.UserMapper;
import cn.tedu.baking.pojo.dto.UserChangeDTO;
import cn.tedu.baking.pojo.dto.UserLoginDTO;
import cn.tedu.baking.pojo.dto.UserRegDTO;
import cn.tedu.baking.pojo.dto.UserUpdateDTO;
import cn.tedu.baking.pojo.entity.User;
import cn.tedu.baking.pojo.vo.ContentAdminVO;
import cn.tedu.baking.pojo.vo.UserAdminVO;
import cn.tedu.baking.pojo.vo.UserVO;
import cn.tedu.baking.response.StatusCode;
import cn.tedu.baking.security.CustomUserDetails;
import cn.tedu.baking.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PasswordEncoder passwordEncoder;
    @Override
    public void reg(UserRegDTO userRegDTO) {
        UserVO userVO = userMapper.selectByUsername(userRegDTO.getUsername());
        if (userVO!=null){
            throw new ServiceException(StatusCode.USERNAME_ALREADY_EXISTS);
        }
        User user = new User();
        BeanUtils.copyProperties(userRegDTO,user);
        //对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setCreateTime(new Date());
        user.setIsAdmin(0);
        userMapper.insert(user);
    }

    @Autowired
    AuthenticationManager manager;
    @Override
    public CustomUserDetails login(UserLoginDTO userLoginDTO) {
        //开启认证流程
        Authentication result = manager.authenticate(
                new UsernamePasswordAuthenticationToken(
                        userLoginDTO.getUsername(),
                        userLoginDTO.getPassword()
                ));
        //把认证结果保存到Security上下文
        SecurityContextHolder.getContext().setAuthentication(result);
        //得到登录成功时的当事人信息
        CustomUserDetails ud = (CustomUserDetails) result.getPrincipal();
        return ud;
    }

    @Override
    public void logout() {
        //清除Security框架上下文中的数据
        SecurityContextHolder.clearContext();
    }

    @Override
    public void update(UserUpdateDTO userUpdateDTO) {

        User user = new User();
        BeanUtils.copyProperties(userUpdateDTO,user);
        userMapper.update(user);

    }

    @Override
    public List<UserAdminVO> list() {
        return userMapper.selectList();
    }
    @Autowired
    ContentMapper contentMapper;
    @Autowired
    CommentMapper commentMapper;

    @Override
    public void deleteById(Long id) {
        commentMapper.deleteByUserId(id);
        contentMapper.deleteByUserId(id);
        userMapper.deleteById(id);
    }

    @Override
    public void change(UserChangeDTO userChangeDTO) {
        User user = new User();
        BeanUtils.copyProperties(userChangeDTO,user);
        userMapper.update(user);
    }


}
